﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Text;

using ECCIT;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.DALFactory;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.DAL;
using OgilvyOne.UKNow.Utility;

namespace OgilvyOne.UKNow.BLL
{
    public class EventsImageBLL
    {
        public int LanguageID { get; set; }
        public EventsImageBLL(int langID)
        {
            this.LanguageID = langID;
        }

        public ResultInfo Save(EventsImageModel info)
        {
            IDALEventsImage use = EventsImageDALFactory.Create(LanguageID);
            info.LanguageID = LanguageID;
            return use.Save(info);
        }
        //删除单个图片的数据
        public ResultInfo delImage(int eid)
        {
            IList<int?> list = new List<int?>();
            list.Add(eid);
            IDALEventsImage use = EventsImageDALFactory.Create(LanguageID);
            return use.Delete(list);
        }
        //根据图片id 获取该图片信息
        public EventsImageModel selImage(int id)
        {
            IDALEventsImage use = EventsImageDALFactory.Create(LanguageID);
            return use.GetEventsImageModelByID(id);
        }

        public ResultInfo DeleteImagesByEventsID(int eventsID)
        {
            return EventsImageDALFactory.Create(LanguageID).DeleteByEventsID(null, eventsID);
        }

        /*修改人：穆凤凤
      * 修改时间：2012-01-02
      * 修改内容：
      *      添加方法,修改某个活动图片的发布状态
      *      有mff注释的为本次修改的代码
      */
        //mff
        //修个单个图片的发布状态
        public void modPubOne(int eid)
        {
            List<int?> list = new List<int?>();
            list.Add(eid);

            IDALEventsImage use = EventsImageDALFactory.Create(LanguageID);

            EventsImageModel model = use.GetEventsImageModelByID(eid); //查看该图片的当前发布状态
            if (model.State > 0) //该图片的当前为发布状态
            {
                use.UnPublish(list);
                //设置为该图片的state为0 不发布 (传递参数 eid 和 int=0)
            }
            else
            {
                use.Publish(list);
                //设置该图片的state为1 发布     (传递参数 eid 和 int=1)
            }
        }


        public PagingList GetEventsImageModelsByKeywords(string keyword, int pageSize, int currentPageIndex)
        {
            PagingParameter pagingParams = PagingParameterFactory.CreateInstance(true, currentPageIndex, pageSize, null);
            IDALEventsImage use = EventsImageDALFactory.Create(LanguageID);
            return use.GetEventsImageModelsByKeywords(keyword, pagingParams);
        }

        public IList<EventsImageModel> GetAllEventsImageModels(int? eventsID)
        {
            IDALEventsImage use = EventsImageDALFactory.Create(LanguageID);
            return use.GetAllEventsImageModels(eventsID);
        }

        public EventsImageModel GetAllEventsImageModelsByIds(int? eventsID)
        {
            IDALEventsImage use = EventsImageDALFactory.Create(LanguageID);
            return use.GetEventsImageModelByID(eventsID);
        }

        //public IList<EventsImageModel> GetTop7EventsImageModels(int? eventsID)
        //{
        //    IDALEventsImage use = EventsImageDALFactory.Create(LanguageID);
        //    IList<EventsImageModel> list = use.GetAllEventsImageModels(eventsID);
        //    if(list.Count>7){
        //        return ((List<EventsImageModel>)list).GetRange(0, 7);
        //    }
        //    return use.GetAllEventsImageModels(eventsID);
        //}

        public IList<EventsImageModel> GetPublishedEventsImageModels(int? eventsID)
        {
            return EventsImageDALFactory.Create(this.LanguageID).GetPublishedEventsImageModels(eventsID);
        }

        public IList<EventsImageModel> GetPublishedGalleryImageModelsByEventsID(int? eventsID)
        {
            return EventsImageDALFactory.Create(this.LanguageID).GetPublishedGalleryImageModelsByEventsID(eventsID);
        }

        public IList<EventsImageModel> GetPublishedGalleryImageModels()
        {
            return EventsImageDALFactory.Create(this.LanguageID).GetPublishedGalleryImageModels();
        }

        private string XMLBuild(ImgXML imgxml)
        {
            XDocument Employee = new XDocument(new XDeclaration("1.0", "utf-8", "true"),
                        new XElement("Image",

                                                new XAttribute("id", imgxml.Id),
                                                            new XAttribute("url", imgxml.Url != null ? imgxml.Url : ""),
                                                            new XAttribute("Eventid", imgxml.Eventid != null ? imgxml.Eventid : ""),
                                                            new XAttribute("EventName", imgxml.EventName != null ? imgxml.EventName : ""),
                                                            new XAttribute("eventStart", imgxml.EventDateStart != null ? imgxml.EventDateStart : ""),
                                                             new XAttribute("eventEnd", imgxml.EventDateEnd != null ? imgxml.EventDateEnd : ""),
                                                            new XAttribute("eventCity", imgxml.Eventcity != null ? imgxml.Eventcity : ""),
                                                            new XAttribute("eventArt_Form", imgxml.EventArt_Form != null ? imgxml.EventArt_Form : "")
                                                ));

            return Employee.ToString();


            //return String.Format(" <Image id={0}  url={1} eventId={2} eventName={3} eventStart={4} eventEnd={5} eventCity={6} event_Art_Form='{7}'/>\n",
                                             //imgxml.Id, imgxml.Url, imgxml.Eventid, imgxml.EventName, imgxml.EventDateStart, imgxml.EventDateEnd, imgxml.Eventcity, imgxml.EventArt_Form);


        }
        public string GetALLRelImgsXML(int langId)
        {
           ImgXML imgxml = null;
           IList<EventsImageModel> iLeim= EventsImageDALFactory.Create(this.LanguageID).GetAllPublishedEventsRecoImageModels();
           StringBuilder stb = new StringBuilder();
       
           foreach (EventsImageModel eim in iLeim)
           {
               EventsModel imX = GetMod(Convert.ToInt32(eim.EventsID), langId);
               if(imX!=null)
               {
               imgxml = new ImgXML();
               imgxml.Url = eim.Path;
               imgxml.Id = eim.ID.ToString();
               imgxml.Eventid = eim.EventsID.ToString();
               imgxml.EventName = imX.Title;
               imgxml.EventDateStart = imX.StartTime.ToString();
               imgxml.EventDateStart = imX.EndTime.ToString();
               imgxml.Eventcity = (
               langId == 2052 ? DictionaryUtil.GetCityByID(Convert.ToInt32(imX.City)).Cnname : DictionaryUtil.GetCityByID(Convert.ToInt32(imX.City)).Enname);
               stb.Append(  XMLBuild(imgxml));
              }

              
           }
           return stb.Append("</images>").Insert(0,String.Format("<images count='{0}'>",iLeim.Count)).ToString();

        }
        private  EventsModel GetMod(int id,int langID)
        {

            return EventsDALFactory.Create(langID).GetEventsModelByID(id);
        }
    }
}
